<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/24
 * Time: 15:46
 */

namespace Biz\module\Classes\mgt;

use Biz\model\trade\OrderPay as mOrderPay;
use Biz\pockets\excel\DownExcel;

class OrderPayExcel
{


    private static $fileName = '支付订单列表'; //Excel文件名
    private static $sheetTitle = '好好住订单'; //Excel的sheet标题

    /**
     * 订单下载
     * @throws \Exception
     */
    public static function downList($field, $orderPayWhere)
    {

        $orderPay = mOrderPay::getOrderPay($field, $orderPayWhere);

        if (!$orderPay) {
            throw new \Exception('暂无可导出数据', 0);
        }

        $orderStatus = mOrderPay::PAY_STATUS_DESC;
        $orderPayDesc = mOrderPay::_PAY_WAYS_DESC;
        $orderPay = self::formatOrderPay($orderPay, $orderStatus, $orderPayDesc);

        $header = [
            '支付渠道',
            '支付时间',
            '支付平台交易号',
            '支付金额',
            '状态',
            '好好住订单号'
        ];
        $letter = range('A', 'F');
        $excelObj = new DownExcel();
        $excelObj->setFileName(self::$fileName);
        $excelObj->setSheetTitle(self::$sheetTitle);
        //设置列宽
        $widths = [
            'A' => 20,
            'B' => 30,
            'C' => 50,
            'D' => 10,
            'E' => 20,
            'F' => 50
        ];
        $excelObj->setWidth($widths);
        //默认单元格居中
        $excelObj->setDefaultStyleAlignment();
        $excelObj->setTextFormat('C'); //支付平台交易号防止为科学计数法
        $excelObj->setTextFormat('F'); //好好住订单号防止为科学计数法
        foreach ($header as $key => $value) {
            //设置字体
            $excelObj->setFrontName($letter[$key]);
            //设置加粗
            $excelObj->setFrontBold($letter[$key]);
            //设置大小
            $excelObj->setFrontSize($letter[$key]);
        }

        $excelObj->create($orderPay, $header);
    }

    protected static function formatOrderPay($orderPay, $orderStatus, $orderPayDesc)
    {

        foreach ($orderPay as $orderPay_k => $orderPay_v) {
            foreach ($orderStatus as $orderStatus_k => $orderStatus_v) {
                if ($orderPay_v['status'] == $orderStatus_k) {
                    $orderPay[$orderPay_k]['status'] = $orderStatus_v;
                }
            }

            foreach ($orderPayDesc as $orderPayDesc_k => $orderPayDesc_v) {
                if (in_array($orderPay_v['pay_way'], mOrderPay::_WECHAT_PAY_WAYS_LIST)) {
                    $orderPay[$orderPay_k]['pay_way'] = mOrderPay::_PAY_WAYS_DESC[mOrderPay::_PAY_WAYS_WECHAT];
                } else if (in_array($orderPay_v['pay_way'], mOrderPay::_ALIPAY_PAY_WAYS_LIST)) {
                    $orderPay[$orderPay_k]['pay_way'] = mOrderPay::_PAY_WAYS_DESC[mOrderPay::_PAY_WAYS_ALIPAY];
                } else if ($orderPay_v['pay_way'] == $orderPayDesc_k) {
                    $orderPay[$orderPay_k]['pay_way'] = $orderPayDesc_v;
                }
            }
        }
        return $orderPay;
    }

}